package com.weijian.controller.admin;

import com.weijian.common.exception.ServiceException;
import com.weijian.dal.dto.PayApplyDTO;
import com.weijian.dal.dto.PayOrderFormDTO;
import com.weijian.dal.enums.PayType;
import com.weijian.service.PayOrderService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("pay-orders")
@RequiredArgsConstructor
public class PayController {

    private final PayOrderService payOrderService;

    @Operation(summary = "生成支付单")
    @PostMapping
    public String applyPayOrder(@RequestBody PayApplyDTO applyDTO) {
        if (PayType.BALANCE.getValue()!=applyDTO.getPayType()) {
            // 目前只支持余额支付
            throw new ServiceException(401, "抱歉，目前只支持余额支付");
        }
        return payOrderService.applyPayOrder(applyDTO);
    }

    @Operation(summary = "尝试基于用户余额支付")
    @PostMapping("{id}")
    public void tryPayOrderByBalance(@PathVariable("id") Long id, @RequestBody PayOrderFormDTO payOrderFormDTO) {
        payOrderFormDTO.setId(id);
        payOrderService.tryPayOrderByBalance(payOrderFormDTO);
    }
}
